/*
 * GConfig.java
 * Created on 14/06/2007
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright 2005-2007 VirtualHockey Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.conf;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import com.hockeo.vhbase.error.GRuntimeException;

/**
 * This bean allows accessing the basic configuration of VirtualHockey. The configuration
 * is partly read from a properties file. Additional settings may be injected by using the
 * Spring framework.
 *
 * @version $Id$
 * @author jjanke
 */
public class GConfig implements ResourceLoaderAware
{
  private ResourceLoader d_resourceLoader;
  private Properties     d_properties;
  private String         d_strPropertiesFile;

  /*
   * (non-Javadoc)
   *
   * @see
   * org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework
   * .core.io.ResourceLoader)
   */
  public void setResourceLoader( ResourceLoader resourceLoader )
  {
    d_resourceLoader = resourceLoader;
  }

  /**
   * Sets the path and name of the file containing the main application properties.
   *
   * @param strFile the path pointing to the main application properties file (in a format
   *          that is supported by Spring's Resource framework)
   */
  public void setPropertiesFile( String strFile )
  {
    d_strPropertiesFile = strFile;
  }

  /**
   * This method must be called once all properties are set. It reads the properties and
   * loads them into memory for easy retrieval afterwards.
   */
  public void init()
  {
    Resource resPropsFile = d_resourceLoader.getResource( d_strPropertiesFile );

    d_properties = new Properties();

    try
    {
      d_properties.load( resPropsFile.getInputStream() );
    }
    catch ( IOException ex )
    {
      throw new GRuntimeException( "Failed to load application properties.", ex );
    }
  }

  /**
   * Returns the property value for the given key.
   *
   * @param strKey the key of the property to be retrieved
   * @return the requested property value
   * @throws GRuntimeException if the requested property does not exist
   */
  public String getProperty( String strKey )
  {
    assert d_properties != null : "Application properties were not initialized.";

    String strValue = d_properties.getProperty( strKey );

    if ( strValue == null )
      throw new GRuntimeException( "Failed to retrieve value for property " + strKey );

    return strValue;
  }

  /**
   * Returns the keys of all defined properties which have been read from the
   * application's main properties file.
   */
  public Enumeration<Object> getPropertyKeys()
  {
    return d_properties.keys();
  }
}
